package com.beiing.leafchartdemo.slice;

import com.beiing.leafchat.LeafLineChart;
import com.beiing.leafchat.bean.Axis;
import com.beiing.leafchat.bean.AxisValue;
import com.beiing.leafchat.bean.Line;
import com.beiing.leafchat.bean.PointValue;
import com.beiing.leafchartdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;
import ohos.agp.utils.Color;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

public class ChartSlice extends AbilitySlice {
    LeafLineChart lineChart;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_fragment_chart);
        lineChart = (LeafLineChart) findComponentById(ResourceTable.Id_leaf_chart);
//        EventRunner runner = EventRunner.create(false);
//        MyEventHandler handler=new MyEventHandler(runner);
//        handler.postTask(task, 2000);
        initLineChart();
    }

//    private class MyEventHandler extends EventHandler {
//        private MyEventHandler(EventRunner runner) {
//            super(runner);
//        }
//        // 重写实现processEvent方法
//        @Override
//        public void processEvent(InnerEvent event) {
//            super.processEvent(event);
//        }
//    }
//    Runnable task = new Runnable() {
//        @Override
//        public void run() {
//            initLineChart();
//        }
//    };
    private void initLineChart() {
        Axis axisX = new Axis(getAxisValuesX());
        axisX.setAxisColor(RgbPalette.parse("#33B5E5")).setTextColor(Color.DKGRAY.getValue()).setHasLines(true);
        Axis axisY = new Axis(getAxisValuesY());
        axisY.setAxisColor(RgbPalette.parse("#33B5E5")).setTextColor(Color.DKGRAY.getValue()).setHasLines(true).setShowText(true);
        lineChart.setAxisX(axisX);
        lineChart.setAxisY(axisY);
        List<Line> lines = new ArrayList<>();
        lines.add(getFoldLine());
        lines.add(getCompareLine());
        lineChart.setChartData(lines);

        lineChart.showWithAnimation(3000);

    }

    private List<AxisValue> getAxisValuesX(){
        List<AxisValue> axisValues = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            AxisValue value = new AxisValue();
            value.setLabel(i + "月");
            axisValues.add(value);
        }
        return axisValues;
    }

    private List<AxisValue> getAxisValuesY(){
        List<AxisValue> axisValues = new ArrayList<>();
        for (int i = 0; i < 11; i++) {
            AxisValue value = new AxisValue();
            value.setLabel(String.valueOf(i * 10));
            axisValues.add(value);
        }
        return axisValues;
    }

    private Line getFoldLine(){
        List<PointValue> pointValues = new ArrayList<>();
        SecureRandom random = new SecureRandom();
        for (int i = 1; i <= 12; i++) {
            PointValue pointValue = new PointValue();
            pointValue.setX( (i - 1) / 11f);
            int var = (int) (random.nextFloat() * 100);
            pointValue.setLabel(String.valueOf(var));
            pointValue.setY(var / 100f);
            pointValues.add(pointValue);
        }

        Line line = new Line(pointValues);
        line.setLineColor(RgbPalette.parse("#33B5E5"))
                .setLineWidth(3)
                .setPointColor(Color.YELLOW.getValue())
                .setCubic(true)
                .setPointRadius(3)
                .setFill(false)
                .setHasLabels(true)
                .setLabelColor(RgbPalette.parse("#33B5E5"));
        return line;
    }

    private Line getCompareLine(){
        List<PointValue> pointValues = new ArrayList<>();
        SecureRandom random = new SecureRandom();
        for (int i = 1; i <= 12; i++) {
            PointValue pointValue = new PointValue();
            pointValue.setX( (i - 1) / 11f);
            int var = (int) (random.nextFloat() * 100);
            pointValue.setLabel(String.valueOf(var));
            pointValue.setY(var / 100f);
            pointValues.add(pointValue);
        }

        Line line = new Line(pointValues);
        line.setLineColor(Color.MAGENTA.getValue())
                .setLineWidth(3)
                .setPointColor(Color.MAGENTA.getValue())
                .setCubic(true)
                .setPointRadius(3)
                .setFill(false)
                .setHasLabels(false);
        return line;
    }
}
